package com.rt.easyjava.common.base;

import com.alibaba.fastjson.JSONObject;
import com.rt.easyjava.common.utils.others.StringUtils;
import lombok.Data;
import lombok.ToString;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by wxy on 2017/9/12.
 */
@Data
@ToString
public class UserDetailsDto implements Serializable {
    private static final long serialVersionUID = 1L;

    private String userId;
    private String loginName;
    private String userName;
    private String phone;
    private String userJson;
    private String deptId;
    private String deptCode;
    private String deptName;
    private String companyId;
    private String companyCode;
    private String companyName;
    private String dataLimitMap;
    private String authorities;

    public List<String> getAuthList() {
        if (StringUtils.isNull(authorities)) return new ArrayList<>();
        return Arrays.asList(authorities.split(","));
    }


    // 当前访问的权限, 如：dept:query
    private String auth;

    /**
     * 数据访问边界
     * @return 0：全部数据， 10：机构内部数据 ; 20: 个人； 不存在则返回 0
     */
    public int getDataLimitType() {
        JSONObject jo = JSONObject.parseObject(dataLimitMap);
        Object dataLimitTypeObj = jo.getOrDefault(auth, "0");
        if (StringUtils.isNull(auth)) {
            dataLimitTypeObj = "0";
        }
        int dataLimitType = Integer.parseInt(dataLimitTypeObj.toString());
        // System.out.println("===> dataLimitType-"+auth+":"+dataLimitType);
        return dataLimitType;
    }


    /**
     * json string -> dto
     * @param userDetailsJson
     * @return
     */
    public static UserDetailsDto getUserDetailsDto(String userDetailsJson) {
        UserDetailsDto userDetailsDto = JSONObject.parseObject(userDetailsJson, UserDetailsDto.class);
        return userDetailsDto;
    }

    /**
     * json string -> dto
     * @param userDetailsJson
     * @return
     */
    public static UserDetailsDto getUserDetailsDto(String userDetailsJson, String auth) {
        if (StringUtils.isNull(userDetailsJson)) {
            return null;
        }
        UserDetailsDto userDetailsDto = JSONObject.parseObject(userDetailsJson, UserDetailsDto.class);
        userDetailsDto.setAuth(auth);
        return userDetailsDto;
    }
}
